#include <iostream>
#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs) {
        unordered_map<string, vector<string>> m;
        for (const auto &s: strs) {
            string temp = string(s);
            sort(temp.begin(), temp.end());
            m[temp].push_back(s);
        }
        vector<vector<string>> result(m.size());
        int i = 0;
        for (const auto &item: m) {
            for (const auto &val: item.second) {
                result[i].push_back(val);
            }
            i++;
        }
        return result;

    }
};


int main() {

    Solution solution;
    vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    auto result = solution.groupAnagrams(strs);
    for (const auto &item: result) {
        for (const auto &val: item) {
            cout << val << " ";
        }
        cout << "\n";
    }
    return 0;
}